//
//  YuYueDetailFooterViewFooterView.m
//  FD
//
//  Created by xiaofang wang on 2019/2/13.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//  参考 OrderFooterDownView.h

#import "YuYueDetailFooterViewFooterView.h"

#define kLeftRightMargin FDScaleWidth(60)

//按钮显示状态逻辑
//两个(左边固定显示:联系商家, 右边:0立即支付. 4评价)
//status:0 立即支付
//status:4 评价

//一个
//其他只显示联系商家

@interface YuYueDetailFooterViewFooterView()
//下===========
/**灰色背景view*/
@property (nonatomic,  strong) UIView *bottomView;
/**leftBtn*/
@property (nonatomic,  strong) UIButton *leftBtn;
/**右边按钮*/
@property (nonatomic,  strong) UIButton *rightBtn;

/**一个按钮的情况:联系商家*/
@property (nonatomic, strong) UIButton *centerBtn;
@end


@implementation YuYueDetailFooterViewFooterView
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.bottomView];
    self.backgroundColor = kMainLightGrayColor;
    [self.bottomView addSubview:self.leftBtn];
    
    [self lightGrayButton:self.leftBtn];
    [self lightRedButton:self.rightBtn];
    [self.bottomView addSubview:self.rightBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
//    //按钮
//    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        //        make.size.mas_equalTo(CGSizeMake(100, 30));
//        make.height.mas_equalTo(26);
//        make.width.mas_equalTo(80);
//        make.centerY.equalTo(self.bottomView.mas_centerY);
//        make.left.equalTo(self.bottomView.mas_left).offset(kLeftRightMargin);
//    }];
//
//    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        //        make.size.mas_equalTo(CGSizeMake(100, 30));
//        make.height.mas_equalTo(26);
//        make.width.mas_equalTo(80); //国际化可能会出问题
//        make.centerY.equalTo(self.bottomView.mas_centerY);
//        make.right.equalTo(self.bottomView.mas_right).offset(-kLeftRightMargin);
//    }];
}

#pragma mark - 接口
//!!!!:需要国际化适配
- (void)placeBtnAndHandleInteractionWithStatus:(NSNumber *)status paytype:(nonnull NSNumber *)paytype{
    //A、排放位置
    [self putPlaceWithStatus:status paytype:paytype];
    
    NSString *ljzfStr = NSLocalizedString(Order_PayNow, @"立即支付");
    //B、更新右侧按钮文字
    if ([status isEqualToNumber:@(0)] && [paytype isEqualToNumber:@(0)]) { //待支付:弹框2个按钮
        [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)];
    
        //2019-2-25 防止返回后刷新页面导致页面布局出错
        self.rightBtn.hidden = NO;
    }else if ([status isEqualToNumber:@(1)] && [paytype isEqualToNumber:@(1)]) {
        [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)]; //待支付，弹框1个按钮
        
        //2019-2-25 防止返回后刷新页面导致页面布局出错
        self.rightBtn.hidden = NO;
    }else if([status isEqualToNumber:@(4)]){
        [self.rightBtn setTitle:NSLocalizedString(L_Order_Evaluate, @"评价") forState:(UIControlStateNormal)];
        
        //2019-2-25防止评价成功之后返回到这个页面，页面布局混乱问题
        self.rightBtn.hidden = NO;
    }else {
        FDLog(@"status:%@-paytype:%@除上述逻辑之外，只显示一个“联系商家”按钮，按钮需要居中显示",status,paytype);
        
        
        //2019-2-25防止评价成功之后返回到这个页面，页面布局混乱问题 这个不写真的会有问题
        self.rightBtn.hidden = YES;
        //2019-2-25重新布局一下 这一坨代码可能适配有点问题
        [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.size.mas_equalTo(CGSizeMake(100, 30));
            make.height.mas_equalTo(26);
            
            //2019-2-25 需要考虑:1.国际化显示不全问题 2.评价成功后宽度太大
            make.width.mas_equalTo(120); //mas_greaterThanOrEqualTo(80);
            make.centerY.equalTo(self.bottomView.mas_centerY);
            make.centerX.equalTo(self.bottomView.mas_centerX);
        }];
    }

    //C 交互
}

- (void)putPlaceWithStatus:(NSNumber *)status paytype:(NSNumber *)paytype{
    //显示两个按钮
    if ([status isEqualToNumber:@(4)]) {
        
        [self layoutTwoButton];
    }else if ([status isEqualToNumber:@(0)] &&
              [paytype isEqualToNumber:@(0)]) {
        [self layoutTwoButton];

    }else if ([status isEqualToNumber:@(1)] &&
              [paytype isEqualToNumber:@(1)]) {
        [self layoutTwoButton];
        
    } else  { //居中显示一个按钮(联系商家)
        //按钮
        [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            //        make.size.mas_equalTo(CGSizeMake(100, 30));
            make.height.mas_equalTo(26);
            make.width.mas_greaterThanOrEqualTo(80);
            make.centerY.equalTo(self.bottomView.mas_centerY);
            make.centerX.equalTo(self.bottomView.mas_centerX);
        }];
    }
}


- (void)layoutTwoButton {
    //按钮
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.height.mas_equalTo(26);
        make.width.mas_greaterThanOrEqualTo(80);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.left.equalTo(self.bottomView.mas_left).offset(kLeftRightMargin);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.height.mas_equalTo(26);
        make.width.mas_greaterThanOrEqualTo(80); //国际化可能会出问题
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.right.equalTo(self.bottomView.mas_right).offset(-kLeftRightMargin);
    }];
}


//- (void)updatefooterBtnWith {
//    self.rightBtn.hidden = YES;
//    [self.leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.height.mas_equalTo(26);
////        make.width.mas_equalTo(80);
//        make.centerY.equalTo(self.bottomView.mas_centerY);
//        make.centerX.equalTo(self.bottomView.mas_centerX);
//    }];
//}


#pragma mark - getter
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [UIView new];
    }
    return _bottomView;
}


//getter
//!!!!:需要国际化适配
- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn =  [self createBtnWithTitle:NSLocalizedString(LianXiShangJia, @"联系商家") action:@selector(clickLeftBtnAction:)];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [self createBtnWithTitle:@"" action:@selector(clickRightBtnAction:)]; //@“再来一单”
    }
    return _rightBtn;
}


- (UIButton *)createBtnWithTitle:(NSString *)title action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    //切边框
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    btn.layer.borderWidth= 1.0;
    //事件
    [btn addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
    
    //15号字体
    btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    return btn;
}

/**更新按钮文字状态 */
- (void)lightGrayButton:(UIButton *)button {
    
    //灰色：999999
    UIColor *hui = [UIColor rb_colorHex:0x999999];
    [button setTitleColor:hui forState:(UIControlStateNormal)];
    button.layer.borderColor = hui.CGColor;
}

- (void)lightRedButton:(UIButton *)button {
    //红色：e54b4b
    UIColor *r = [UIColor rb_colorHex:0xe54b4b];
    [button setTitleColor:r forState:(UIControlStateNormal)];
    button.layer.borderColor = r.CGColor;
}

#pragma mark - 点击事件
- (void)clickLeftBtnAction:(UIButton *)btn {
    FDLog(@"点击左 按钮 %s---%d",__FUNCTION__,__LINE__);
    if (self.clickLeftBtnBlock) {
        self.clickLeftBtnBlock();
    }
}

- (void)clickRightBtnAction:(UIButton *)btn {
    FDLog(@"点击右 按钮 %s---%d",__FUNCTION__,__LINE__);
    if (self.clickRightBtnBlock) {
        self.clickRightBtnBlock();
    }
}
@end
